package cn.hefan.web.servlet.user;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.hefan.domain.ResultInfo;
import cn.hefan.domain.User;
import cn.hefan.service.findUserService;


public class addUserServlet extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//解决中午乱码问题
		request.setCharacterEncoding("UTF-8");
		
		Map<String, String[]> map = request.getParameterMap();
		//创建user对象
		User user = new User();
		//封包
		try {
			BeanUtils.populate(user, map);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultInfo info = new ResultInfo(false);
		if(user.getName()!=null&&user.getDivide()!=null&&user.getIdcard()!=null&&user.getPassword()!=null&&user.getUsername()!=null) {
			info = findUserService.addUser(user);
		}
		//响应数据
		ObjectMapper mapper = new ObjectMapper();
				
		response.setContentType("application/json;charset=utf-8");
		mapper.writeValue(response.getOutputStream(),info);
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

}
